class Solution {
    vector<vector<int>> res;
    vector<bool> vis;
public:
    void dfs(vector<int>& num, vector<int>& path) {
        if (path.size() == num.size()) {
            res.push_back(path);
            return;
        }
        for (int i = 0; i < num.size(); ++i) {
            if (vis[i]) continue;
            path.push_back(num[i]);
            vis[i] = true;
            dfs(num, path);
            vis[i] = false;
            path.pop_back();
        }
    }
    vector<vector<int>> permute(vector<int>& num) {
        sort(num.begin(), num.end());
        vector<int> path;
        vis = vector<bool>(num.size(), false);
        dfs(num, path);
        return res;
    }
};